﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.MSE.Catalog.Contracts;
using System.ServiceModel.Channels;
using System.Threading;
using DTO = Microsoft.MSE.Catalog.Contracts;
using Bus = Microsoft.MSE.Catalog.Module.Business;

namespace Microsoft.MSE.Catalog.Module
{
    public class MSENotificationManager : INotificationManager
    {
        #region INotificationManager Members

        public void RaiseEvent(MseEventData eventData)
        {
            RuntimeServerNotificationList list = Data.RuntimeServer.ListRuntimeServerNotificationAddresses(null, null);
            if (list == null || list.RuntimeServers == null || list.RuntimeServers.Count == 0)
                return;

            lock (this)
            {
                foreach (RuntimeServerNotificationListElement element in list.RuntimeServers)
                {
                    DTO.MSEBinding mseBinding = Bus.BindingManager.GetMSEBinding(element.BindingID, null, null);
                    Binding binding = BindingHelper.DeserializeBinding(mseBinding.BindingConfig);
                    Notification notification = new Notification(eventData, element.NotificationUrl,
                                                                SC.CatalogCallbackAcceptEventSoapAction,
                                                                string.Empty,
                                                                binding);
                    Thread thread = new Thread(new ThreadStart(notification.SendOneWayNotification));
                    thread.Start();
                }
            }
        }

        #endregion
    }
}
